package com.shcd.mapperImpl;

import com.shcd.mapper.ExcelDownloadMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ExcelDownloadMapperImpl implements ExcelDownloadMapper {

    private final ExcelDownloadMapper excelDownloadMapper;

    @Autowired
    public ExcelDownloadMapperImpl(ExcelDownloadMapper excelDownloadMapper) {
        this.excelDownloadMapper = excelDownloadMapper;
    }

    @Override
    public List<Map<String, String>> getData_ywlj() {
        return disposeData(excelDownloadMapper.getData_ywlj());
    }

    @Override
    public List<Map<String, String>> getData_fgj() {
        return disposeData(excelDownloadMapper.getData_fgj());
    }

    private List<Map<String, String>> disposeData(List<Map<String, String>> data) {
        //保存所有单位的每个单位有多少个目录，key是单位名称
        Map<String, Integer> sum = new HashMap<>();
        //保存所有单位的每个单位所有未更新时间总和，key是单位名称
        Map<String, Float> notUpdate_sum = new HashMap<>();
        for (Map<String, String> datum : data) {
            Set<String> keys = sum.keySet();
            String key = datum.get("COMPANYNAME");
            String notUpdate_key = datum.get("NOTUPDATEDAY");
            if (keys.contains(key)) {
                //map中有这个单位则+1
                sum.put(key, sum.get(key) + 1);
                if (notUpdate_key != null)
                    notUpdate_sum.put(key, notUpdate_sum.get(key) + Math.max(0, disposeTime(notUpdate_key) - getPeriod(datum.get("COLLECTIONPERIOD"))));
                else
                    datum.put("NOTUPDATEDAY", "-");
            } else {
                //map中没有则新建然后赋值为1
                sum.put(key, 1);
                if (notUpdate_key != null)
                    notUpdate_sum.put(key, Math.max(0, disposeTime(notUpdate_key) - getPeriod(datum.get("COLLECTIONPERIOD"))));
                else {
                    notUpdate_sum.put(key, 0f);
                    datum.put("NOTUPDATEDAY", "-");
                }
            }
        }
        for (int i = 0; i < data.size(); ++i) {
            int s = sum.get(data.get(i).get("COMPANYNAME"));
            float ss = notUpdate_sum.get(data.get(i).get("COMPANYNAME")) / s;
            //String time = data.get(i).get("UPDATETIME").equals("-") ? "-" : data.get(i).get("UPDATETIME").substring(0,data.get(i).get("UPDATETIME").indexOf("."));
            String time = data.get(i).get("UPDATETIME");
            String notUpdate_time = data.get(i).get("NOTUPDATEDAY").equals("-") ? "-" : String.valueOf(Math.max(0, disposeTime(data.get(i).get("NOTUPDATEDAY")) - getPeriod(data.get(i).get("COLLECTIONPERIOD"))));
            data.get(i).put("NO", String.valueOf(i + 1));
            data.get(i).put("DIRNUMBER", String.valueOf(s));
            data.get(i).put("UPDATETIME", time);
            data.get(i).put("NOTUPDATEDAY", notUpdate_time);
            data.get(i).put("AVENOTUPDATEDAY", String.valueOf(ss));
        }
        return data;
    }

    private Integer getPeriod(String period) {
        switch (period) {
            case "每天":
                return 1;
            case "每周":
                return 7;
            case "每月":
                return 30;
            case "每季度":
                return 90;
            case "每半年":
                return 180;
            case "每年":
                return 360;
            case "即时":
            case "其他":
            default:
                return 0;
        }
    }

    private float disposeTime(String time) {
        int day = Integer.parseInt(time.substring(0, time.indexOf("天")));
        int hour = Integer.parseInt(time.substring(time.indexOf("天") + 1, time.indexOf("时")));
        int minute = Integer.parseInt(time.substring(time.indexOf("时") + 1, time.indexOf("分")));
        return day + hour / 24f + minute / 1440f;
    }
}
